package com.dao.Impl;

import com.dao.CateGoryDao;
import com.entity.Cardtype;
import com.entity.Category;
import com.entity.PageBean;
import com.util.JDBC;
import com.util.TransBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CateGoryDaoImp implements CateGoryDao {
    @Override
    public int addCate(Category cg) {
            return JDBC.sqlUpdate("insert into Category values(null,?,?,?)", cg.getName(), cg.getStatus(), cg.getMomo());
    }

    @Override
    public List<Category> findAll() {
        List<Category> clist = null;
            List<Map> maps =  JDBC.selectMap("select id,name,status,momo from category");
            clist = TransBean.populate(Category.class,maps);
        return clist;
    }

    @Override
    public void findPage(PageBean pageBean) {
        String sql = "select count(*) from category where 1=1 ";
        String factor = "";
        if (pageBean.getFactor() != null) {
            for (String s : pageBean.getFactor()) {
                factor += " and " + s;
            }
        }
        List<List> lists = JDBC.selectArray(sql + factor);
        if (lists.size() > 0) {
            pageBean.setRowCount(((Long) lists.get(0).get(0)).intValue());
        }
        pageBean.setTotalPage();
        pageBean.setStartIndex();

        sql = "select id,name,status,momo from category where 1=1 %s limit ?,?";
        sql = String.format(sql, factor);
        List<Map> mapList = JDBC.selectMap(sql, pageBean.getStartIndex(), pageBean.getPageSize());
        List<Category> cgs = TransBean.populate(Category.class, mapList);
        pageBean.setData(cgs);

    }

    @Override
    public Category findById(int id) {
        List<List> lists = JDBC.selectArray("select id,name,status,momo from category where id=?", id);
        if (lists.size() > 0) {
            List li = lists.get(0);
            return new Category(
                    (Integer) li.get(0),
                    (String) li.get(1),
                    (Integer) li.get(2),
                    (String) li.get(3)
            );
        }
        return null;
    }

    @Override
    public int modify(Category cg) {
            return JDBC.sqlUpdate("Update Category set name=?,status=?,momo=? where id=?",
                    cg.getName(),
                    cg.getStatus(),
                    cg.getMomo(),
                    cg.getId()
            );
    }

    @Override
    public int del(int id) {
            return JDBC.sqlUpdate("delete from Category where id=?", id);
    }

    @Override
    public List<Map> findName() {
        return JDBC.selectMap("select name from category");
    }

    @Override
    public int findId(String name) {
        List<List> lists = JDBC.selectArray("select id from category where name=?", name);
        return (Integer)lists.get(0).get(0);
    }

//    @Override
//    public Category findByIdStatus(int id) {
//        List<List> lists = JDBC.selectArray("select id,name,status,momo from Category where id=?", id);
//        if (lists.size() > 0) {
//            List li = lists.get(0);
//            return new Category(
//                    (Integer) li.get(0),
//                    (String) li.get(1),
//                    (Integer) li.get(2),
//                    (String) li.get(3)
//            );
//        }
//        return null;
//    }

}
